<template>
  <el-dialog width="80%" :title="titleMap[status]" :visible.sync="visible" :close-on-click-modal="false" @open="open" @close="$emit('update:show', false)">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="工作名称" prop="master_id">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="类型" prop="typ">
        <el-select
          v-model="form.typ"
          placeholder="选择类型"
        >
          <el-option
            v-for="item in typList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模式" prop="mode">
        <el-select
          v-model="form.mode"
          placeholder="选择模式"
        >
          <el-option
            v-for="item in modeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主机" prop="host">
        <el-radio-group v-model="form.host_id">
          <el-radio v-for="item in hostList" :key="item.id" :label="item.id">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="线路" prop="line">
        <el-select
          v-model="form.line"
          placeholder="选择线路"
        >
          <el-option
            v-for="item in lineList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="额外信息" prop="script_path">
        <el-input v-model="form.extra" placeholder="组队填写队长、仓库填写材料名、采集填写材料名" style="width: 500px" />
      </el-form-item>
      <el-form-item label="功能" prop="line">
        <el-radio-group v-model="form.script_id">
          <el-radio v-for="item in scriptList" :key="item.id" :label="item.id">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="配置" prop="json_path">
        <el-radio-group v-model="form.json_id">
          <el-radio v-for="item in jsonList" :key="item.id" :label="item.id">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="设置">
        <el-checkbox v-model="form.auto_restart" style="margin-left: 20px">自动重启</el-checkbox>
        <el-checkbox v-model="form.injure_stop">受伤停止</el-checkbox>
        <el-checkbox v-model="form.soul_stop">丢魂停止</el-checkbox>
        <el-checkbox v-model="form.auto_login" style="margin-left: 20px">自动登录</el-checkbox>
        <el-checkbox v-model="form.auto_change_line" style="margin-left: 20px">自动换线</el-checkbox>
        <el-checkbox v-model="form.skip_update" style="margin-left: 20px">禁用登录器更新</el-checkbox>
      </el-form-item>
      <el-table
        :data="users"
      >
        <el-table-column
          prop="master_id"
          label="主账号"
        />
        <el-table-column
          prop="slave_id"
          label="子账号"
        />
        <el-table-column
          prop="name"
          label="名称"
        />
        <el-table-column
          prop="job"
          label="职业"
        />
      </el-table>
      <el-form-item>
        <el-button type="primary" @click="opObj">确定</el-button>
        <el-button @click="cancelDialog">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  createWorkApi as createObjApi,
  updateWorkApi as updateObjApi,
  getWorkApi as getObjApi
} from '@/api/workflow'
import {
  getHostListApi
} from '@/api/host'
import {
  getScriptListApi
} from '@/api/script'
import {
  getSettingListApi
} from '@/api/setting'
export default {
  name: 'FormDialog',
  props: {
    objId: {
      type: String,
      default: null
    },
    status: {
      required: true,
      type: String
    },
    show: {
      type: Boolean,
      default: false
    },
    users: {
      type: Array,
      default: () => { [] }
    }
  },
  data() {
    return {
      loading: false,
      visible: this.show,
      titleMap: {
        create: '创建',
        update: '编辑'
      },
      typList: [
        { name: '练级', value: '练级' },
        { name: '采集', value: '采集' },
        { name: '收货', value: '收货' },
        { name: '发货', value: '发货' },
        { name: '买布', value: '买布' }
      ],
      modeList: [
        { name: '单人', value: '单人' },
        { name: '组队', value: '组队' }
      ],
      lineList: [
        { name: '随机', value: 0 },
        { name: '1线', value: 1 },
        { name: '2线', value: 2 },
        { name: '3线', value: 3 },
        { name: '4线', value: 4 },
        { name: '5线', value: 5 },
        { name: '6线', value: 6 },
        { name: '7线', value: 7 },
        { name: '8线', value: 8 },
        { name: '9线', value: 9 },
        { name: '10线', value: 10 }
      ],
      hostList: [],
      scriptList: [],
      jsonList: [],
      form: {
        name: '',
        typ: '练级',
        mode: '组队',
        line: 1,
        host_id: null,
        injure_stop: false,
        soul_stop: false,
        auto_restart: true,
        auto_login: true,
        auto_change_line: false,
        skip_update: true,
        json_path: '',
        script_path: '',
        extra: '',
        users: this.users
      }
    }
  },
  watch: {
    show: function() {
      this.visible = this.show
    },
    users: function() {
      this.form.users = this.users
    }
  },
  methods: {
    open() {
      this.getScriptList()
      this.getJsonList()
      this.getHostList()
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.resetForm()
      if (this.status === 'update') {
        this.getObj()
      }
    },
    getScriptList() {
      const data = {}
      getScriptListApi(data).then(resp => {
        if (resp.code === 0) {
          this.scriptList = resp.data.data_list
        }
      })
    },
    getJsonList() {
      const data = {}
      getSettingListApi(data).then(resp => {
        if (resp.code === 0) {
          this.jsonList = resp.data.data_list
        }
      })
    },
    getHostList() {
      const data = {}
      getHostListApi(data).then(resp => {
        if (resp.code === 0) {
          this.hostList = resp.data.data_list
          if (this.hostList.length > 0) {
            this.form.host_id = this.hostList[0].id
          }
        }
      })
    },
    getObj() {
      this.loading = true
      const data = {
        obj_id: parseInt(this.objId)
      }
      getObjApi(data).then(resp => {
        if (resp.code === 0) {
          this.form = resp.data
        }
        this.loading = false
      })
    },
    resetForm() {
      this.form.name = ''
      this.form.typ = '练级'
      this.form.mode = '组队'
      this.form.line = 1
      this.form.host_id = null
      this.form.script_path = ''
      this.form.json_path = ''
      this.form.injure_stop = false
      this.form.soul_stop = false
      this.form.auto_restart = true
      this.form.auto_login = true
      this.form.auto_change_line = false
      this.form.skip_update = true
      this.form.extra = ''
    },
    cancelDialog() {
      this.$emit('update:show', false)
    },
    opObj() {
      if (this.status === 'create') {
        this.createObj()
      } else if (this.status === 'update') {
        this.updateObj()
      }
    },
    createObj() {
      console.log(this.form)
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          const data = this.form
          createObjApi(data).then(resp => {
            if (resp.code === 0) {
              this.$emit('update:show', false)
              this.$notify({
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
              this.$emit('success')
            }
            this.loading = false
          })
        }
      })
    },
    updateObj() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          const data = this.form
          data['obj_id'] = parseInt(this.objId)
          updateObjApi(data).then(resp => {
            if (resp.code === 0) {
              this.$emit('update:show', false)
              this.$notify({
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
              this.$emit('success')
            }
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
